﻿using DevExpress.XtraGrid.Views.Grid;
using RecruitStudents.Client.BLL;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Base
{
    public partial class Base_ExamPlace_SpecialityKinds_Group_List : BaseForm
    {
        ServiceBase service = new ServiceBase();
        public Base_ExamPlace_SpecialityKinds_Group_List()
        {
            InitializeComponent();
            BindExamPlace();
            BindExamLevel();
            BindGridList();
        }

        private void BindExamLevel()
        {
            Dictionary<string, string> dicExamLevel = Utility.Const.ConstExamLevel.DicExamLevel;
            dicExamLevel.Remove("0");
            listExamLevel.DisplayMember = "Value";
            listExamLevel.ValueMember = "Key";
            listExamLevel.DataSource = dicExamLevel.ToList();
        }

        private void BindExamPlace()
        {
            listExamPlace.DataSource = null;
            DataTable dt = service.GetExamPlaceSpecialityKindsMappingTable("","");
            if (dt != null)
            {
                DataTable NewDt = new DataTable();
                NewDt.Columns.Add(new DataColumn("name"));
                NewDt.Columns.Add(new DataColumn("code"));
                NewDt.Columns.Add(new DataColumn("SpecialityKindsNO"));
                foreach (DataRow dr in dt.Rows)
                {
                    DataRow row = NewDt.Rows.Add();
                    row["name"] = dr["ExamPlaceName"].ToSafeString() + "-" + dr["SpecialityKindsName"].ToSafeString();
                    row["code"] = dr["ExamPlaceSpecialityKindsMappingID"].ToSafeString();
                    row["SpecialityKindsNO"] = dr["SpecialityKindsNO"].ToSafeString();
                }
                listExamPlace.DataSource = NewDt;         // 绑定数据源
                listExamPlace.DisplayMember = "name";  // 指定显示字段
                listExamPlace.ValueMember = "code";    // 指定值字段
            }
        }

        private void BindGridList()
        {
            if (listExamPlace.SelectedIndex < 0)
            {
                return;
            }
            if (listExamLevel.SelectedIndex < 0)
            {
                return;
            }
            string ExamPlaceSpecialityKindsMappingID = string.Empty;
            string ExamLevel = string.Empty;
            if (listExamPlace.SelectedItem != null)
            {
                DataRowView drExamPlace = (DataRowView)listExamPlace.SelectedItem;
                ExamPlaceSpecialityKindsMappingID = drExamPlace.Row.ItemArray[1].ToSafeString();
            }
            if (listExamLevel.SelectedItem != null)
            {
                KeyValuePair<string, string> key = (KeyValuePair<string, string>)listExamLevel.SelectedItem;
                ExamLevel = key.Key.ToSafeString();
            }          
            DataTable dt = service.GetExamPlaceSpecialityKindsGroupTable(ExamPlaceSpecialityKindsMappingID, string.Empty, "", ExamLevel);
          
            dataGridViewX1.DataSource = dt;
        }

        private void listExamPlace_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridList();
        }

        private void listExamLevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridList();
        }

      
        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.GetFocusedValue() != null)
            {
                string strExamPlaceSpecialityKindsMappingID = string.Empty;
                string strExamPlaceNO = string.Empty;
                string strExamLevelNo = string.Empty;
                GridView rowsSelected = gridView1 as GridView;
                if (rowsSelected.Rows.Count > 0)
                {
                    int[] inteurm = rowsSelected.GetSelectedRows();
                    if (inteurm != null && inteurm.Length > 0)
                    {
                        strExamPlaceSpecialityKindsMappingID = rowsSelected.Rows[inteurm[0]].DataRow.ItemArray[0].ToString();
                        strExamPlaceNO = rowsSelected.Rows[inteurm[0]].DataRow.ItemArray[1].ToString();
                        strExamLevelNo = rowsSelected.Rows[inteurm[0]].DataRow.ItemArray[3].ToString();
                        Base_ExamPlace_SpecialityKinds_Group_Option option = new Base_ExamPlace_SpecialityKinds_Group_Option(strExamPlaceSpecialityKindsMappingID, strExamPlaceNO, strExamLevelNo);
                        if (option.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            BindGridList();
                        }

                    }
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            string strExamPlaceSpecialityKindsMappingID = string.Empty;
            string strGroupNO = string.Empty;
            GridView rowsSelected = gridView1 as GridView;
            if (rowsSelected.Rows.Count > 0)
            {
                int[] inteurm = rowsSelected.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    strExamPlaceSpecialityKindsMappingID = rowsSelected.Rows[inteurm[0]].DataRow.ItemArray[0].ToString();
                    strGroupNO = rowsSelected.Rows[inteurm[0]].DataRow.ItemArray[1].ToString();
                    service.DeleteExamPlaceSpecialityKindsGroupMapping(strExamPlaceSpecialityKindsMappingID, strGroupNO);
                    BindGridList();
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (listExamPlace.SelectedIndex < 0)
            {
                DevHelper.ShowErrorMessageBox("请先选择考点专业!");
                listExamPlace.Focus();
                return;
            }
            if (listExamLevel.SelectedIndex < 0)
            {
                DevHelper.ShowErrorMessageBox("请先选择专业级别!");
                listExamLevel.Focus();
                return;
            }
            string ExamPlaceSpecialityKindsMappingID = string.Empty;
            string ExamLevel = string.Empty;
            if (listExamPlace.SelectedItem != null)
            {
                DataRowView drExamPlace = (DataRowView)listExamPlace.SelectedItem;
                ExamPlaceSpecialityKindsMappingID = drExamPlace.Row.ItemArray[1].ToSafeString();
            }
            if (listExamLevel.SelectedItem != null)
            {
                KeyValuePair<string, string> key = (KeyValuePair<string, string>)listExamLevel.SelectedItem;
                ExamLevel = key.Key.ToSafeString();
            }
            Base_ExamPlace_SpecialityKinds_Group_Option option = new Base_ExamPlace_SpecialityKinds_Group_Option(ExamPlaceSpecialityKindsMappingID, string.Empty, ExamLevel);
            if (option.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BindGridList();
            }
        }
    }
}
